<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            width: 670px;
            height: 290px;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: all 1s;
        }

        .img-wrapper {
            position: relative;
            width: 670px;
            height: 290px;
        }
    </style>
</head>

<body>
    <div class="lunbo">
        <div class="img-wrapper">
            <img style="opacity:1" src="lunbo1.jpg" alt="">
            <img src="lunbo2.jpg" alt="">
            <img src="a1.jpg" alt="">
        </div>
        <button onclick="showPrev()">上一张</button>
        <button onclick="showNext()">下一张</button>
    </div>
    <script>
        // 找到所有img对象
        var imgs = document.querySelectorAll('img');
        // 要显示的图片下标
        var j = 0;


        function showPrev() {
            j--;
            if (i < 0) { // 直接显示最后一张
                j = imgs.length - 1;
            }
            showImg();
        }
        function showNext() {
            j++;
            if (j >= imgs.length) { // 超过最后一张直接显示第一张
                j = 0;
            }
            showImg();
        }
        function showImg() {
            for (var i = 0; i < imgs.length; i++) {
                if (i != j) {
                    // 将所有的图片隐藏
                    imgs[i].style.opacity = 0;
                    imgs[j].style.zIndex = 98;
                }
            }
            imgs[j].style.opacity = 1;
            imgs[j].style.zIndex = 99; // 显示在最上面
        }
        showImg(); // 默认显示一张
    </script>
</body>

</html>